package com.mzba.easy.weibo.ui;

import java.util.List;

import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.view.MenuItem;
import com.mzba.easy.weibo.BasicActivity;
import com.mzba.easy.weibo.R;
import com.mzba.easy.weibo.db.CommentEntry;
import com.mzba.easy.weibo.db.CommentGson;
import com.mzba.easy.weibo.util.UICore;
import com.mzba.easy.weibo.util.WeiboManager;
import com.mzba.easy.weibo.widget.CustomListView;
/**
 * 
 * @author 06peng
 *
 */
public class CommentsActivity extends BasicActivity implements Callback {
	
	private Handler handler;
	public static final int init_comments = 1;
	private final int get_more_comments = 2;
	private String id;
	private WeiboManager weiboManager;
	private List<CommentEntry> entries;
	private int count = 20;
	private int page = 1;
	private CustomListView listView;
	private CommentListAdapter adapter;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.comment_list);
        handler = new Handler(this);
        weiboManager = new WeiboManager(this);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setTitle("评论列表");
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayShowHomeEnabled(false);
        initWidget();
        
        id = getIntent().getStringExtra("id");
        UICore.eventTask(this, this, init_comments, "", null);
    }
    
    private void initWidget() {
        listView = (CustomListView) findViewById(R.id.status_timeline_scrollview);
        listView.setonRefreshListener(new CustomListView.OnRefreshListener() {
			
			@Override
			public void onRefresh() {
				page = 1;
				UICore.eventTask(CommentsActivity.this, CommentsActivity.this, init_comments, null, null);
			}
		});
        listView.setonLoadListener(new CustomListView.OnLoadListener() {
			
			@Override
			public void onLoad() {
				page++;
				UICore.eventTask(CommentsActivity.this, CommentsActivity.this, get_more_comments, null, null);
			}
		});
    	
    }
    
    @Override
    public void execute(int mes, Object obj) {
    	CommentGson gson = null;
    	switch (mes) {
		case init_comments:
			gson = weiboManager.getCommentsByStatusid(count, page, id);
			entries = gson.getComments();
			if (entries != null && !entries.isEmpty()) {
				handler.sendEmptyMessage(init_comments);
			}
			break;
		case get_more_comments:
			gson = weiboManager.getCommentsByStatusid(count, page, id);
			List<CommentEntry> more = gson.getComments();
			entries.addAll(more);
			handler.sendEmptyMessage(get_more_comments);
			break;
		default:
			break;
		}
    	
    }


	@Override
	public boolean handleMessage(Message msg) {
		switch (msg.what) {
		case init_comments:
			if (entries != null && !entries.isEmpty()) {
				adapter = new CommentListAdapter(this, entries, listView, id);
				listView.setAdapter(adapter);
			}
			listView.onRefreshComplete();
			break;
		case get_more_comments:
			adapter.notifyDataSetChanged();
			listView.setSelection(adapter.getCount() - count);
			listView.onLoadComplete();
			break;
		default:
			break;
		}
		return false;
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			finish();
			break;

		default:
			break;
		}
		return true;
	}
	
}